package com.bt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bt.mapper.DtsGrouponRulesMapper;
import com.bt.pojo.DtsGoods;
import com.bt.pojo.DtsGrouponRules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/26 16:09
 **/
@Service
public class DtsGrouponRulesServiceImpl implements DtsGrouponRulesService {
    @Autowired
    private DtsGrouponRulesMapper dtsGrouponRulesMapper;
    @Autowired
    private DtsGoodsService dtsGoodsService;
    @Override
    public IPage<DtsGrouponRules> findDtsGrouponrules(Integer goodsId, Integer page, Integer limit, String sort, String order) {
        return dtsGrouponRulesMapper.selectPage(new Page<DtsGrouponRules>(page,limit),new QueryWrapper<DtsGrouponRules>().eq(goodsId!=null?true:false,"goods_id",goodsId).orderBy(true,"desc".equals(order),sort));
    }

    @Override
    public void insertDtsGroupRules(DtsGrouponRules dtsGrouponRules) {
        dtsGrouponRules.setAddTime(new Date());
        dtsGrouponRules.setUpdateTime(new Date());
        DtsGoods dtsGoodById = dtsGoodsService.findDtsGoodById(dtsGrouponRules.getGoodsId());
        if (dtsGoodById==null){
           throw new RuntimeException("商品不存在");
        }
        dtsGrouponRules.setGoodsName(dtsGoodById.getName());
        dtsGrouponRulesMapper.insert(dtsGrouponRules);
    }

    @Override
    public void updateDtsGroupRules(DtsGrouponRules dtsGrouponRules) {
        dtsGrouponRules.setUpdateTime(new Date());
        DtsGoods dtsGoodById = dtsGoodsService.findDtsGoodById(dtsGrouponRules.getGoodsId());
        if (dtsGoodById==null){
            throw new RuntimeException("商品不存在");
        }
        dtsGrouponRules.setGoodsName(dtsGoodById.getName());
        dtsGrouponRulesMapper.updateById(dtsGrouponRules);
    }

    @Override
    public void deleteDtsGroupRules(DtsGrouponRules dtsGrouponRules) {
        dtsGrouponRulesMapper.deleteById(dtsGrouponRules.getId());
    }
}
